//
//  YPWebViewModel.m
//  Youpin
//
//  Created by xiaowuxiaowu on 2017/8/16.
//  Copyright © 2017年 xiaowuxiaowu. All rights reserved.
//

#import "YPWebViewModel.h"

@implementation YPWebViewModel

-(instancetype)init{

    if(self = [super init]){
        self.height = 44.0f;
    }
    return self;
}

-(void)setIndexPath:(NSIndexPath *)indexPath{
    _indexPath = indexPath;
    self.webView.UIDelegate = self;
    self.webView.navigationDelegate = self;
    if(_html.length > 0 && !self.isEnd){
//        [self.webView loadRequest:[NSURLRequest requestWithURL:urlNamed(_html)]];
        [self.webView loadHTMLString:xw_getNULLString(_html) baseURL:nil];
       
    }
}
-(void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error{
    
    DLog(@"=-------error ------%@",error);
    [self.webView tp_startAnimating];
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
    
    
    @weakify(self);
    [webView evaluateJavaScript:@"document.body.scrollHeight" completionHandler:^(id data, NSError * _Nullable error) {
        
        
        @strongify(self);
        self.isEnd = YES;
        self.count ++;
        CGFloat height = [data floatValue];
//        self.webView.height = height+15;
        self.height = height + 40.0f;
//        [self.tableView reloadRowsAtIndexPaths:@[self.indexPath] withRowAnimation:UITableViewRowAnimationNone];
        [self.tableView reloadData];
         [self.webView tp_stopAnimating];
        DLog(@"=-------didFinishNavigation ------%@",data);
    }];
//    NSString *js = @"var imgs = document.getElementsByTagName(\"img\");for (var i=0;i<imgs.length;i++){document.write(imgs[i].src + \"<br>\");}";
//    [self.webView evaluateJavaScript:js completionHandler:^(id _Nullable obj, NSError * _Nullable error) {
//        DLog(@"----obj%@",obj);
//    }];
    
    
//    
//    NSString *js = @"var imgs = document.getElementsByTagName(\"img\");for (var i=0;i<imgs.length;i++){var src = imgs[i].src;imgs[i].src = %@ + src;}";
//    [self.webView evaluateJavaScript:[NSString stringWithFormat:js,TPImageUrl] completionHandler:^(id _Nullable obj, NSError * _Nullable error) {
//        DLog(@"----obj%@",obj);
//    }];
//    
   
    
    
}

@end
